\documentclass[a4paper,oneside]{report}

\usepackage[margin=0.5in]{geometry}
\usepackage{lscape}
\usepackage{pdflscape}
\usepackage{listings}
\usepackage{color}
\usepackage{multicol}
%\usepackage[left=2cm, right=2cm]{geometry} 

\newcommand{\code}[1]{\texttt{#1}}

\lstset{ %
language=JAVA,                % choose the language of the code
basicstyle=\footnotesize,       % the size of the fonts that are used for the code
numbers=left,                   % where to put the line-numbers
numberstyle=\footnotesize,      % the size of the fonts that are used for the line-numbers
stepnumber=10,                   % the step between two line-numbers. If it's 1 each line will be numbered
numbersep=5pt,                  % how far the line-numbers are from the code
backgroundcolor=\color{white},  % choose the background color. You must add \usepackage{color}
showspaces=false,               % show spaces adding particular underscores
showstringspaces=false,         % underline spaces within strings
showtabs=false,                 % show tabs within strings adding particular underscores
frame=single,                    % adds a frame around the code
tabsize=2,                    % sets default tabsize to 2 spaces
captionpos=b,                   % sets the caption-position to bottom
breaklines=true,                % sets automatic line breaking
breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
escapeinside={\%*}{*)}          % if you want to add a comment within your code
}

\begin{document}
\input{../title.tex}
    
\section*{Report}
The approach solution to the problem was given:
\begin{itemize}
   \item flatten control structures,
   \item translate variable declarations,
   \item translate variable references in expressions, and
   \item flatten expressions.
\end{itemize}

\noindent To flatten control structures we followed the following approach (reference: Compiler Design I lecture notes).
\begin{multicols}{2}
\noindent \code{if-else} statement
\begin{verbatim}
if E1 then
   S1
else
   S2



\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
   temp1 = E1
   if not temp1 goto L2
   S1
   goto Lnext
L2:	
   S2
Lnext:	
\end{verbatim}
\end{multicols}

\begin{multicols}{2}
\noindent \code{if} statement
\begin{verbatim}
if E1 then
   S1
   
 
 
\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
   temp1 = E1
   if not temp1 goto Lnext
   S1
Lnext:	
\end{verbatim}
\end{multicols}


\begin{multicols}{2}
\noindent \code{while} statement
\begin{verbatim}
while E do S




\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
    goto Ltest
Lbody:  S
Ltest:  temp = E
    if temp goto Lbody
Lnext:
\end{verbatim}
\end{multicols}

\begin{multicols}{2}
\noindent \code{return} statement
\begin{verbatim}
return E


\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
    temp = E
    tempRV = temp
    goto Lreturn
\end{verbatim}
\end{multicols}

\begin{multicols}{2}
\noindent \code{\&\&} expression
\begin{verbatim}
E1 && E2






\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
   temp1 = E1
   if not temp1 goto L3
   temp1 = E2
   goto Lnext
L3:
   temp1 = 0
Lnext:   
\end{verbatim}
\end{multicols}

\noindent To translate variable declarations we followed the following approach.
\begin{multicols}{2}
Global variables
\begin{verbatim}
Scalar or array.
\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
Label definition with 
the explicit size of 
the variable. 
\end{verbatim}
\end{multicols}

\begin{multicols}{2}
Local variables
\begin{verbatim}
Parameters or 
scalar local variables.

Array local variables.

\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
Temporaries.


Handled in the stack, 
reflected in the frame
size.
\end{verbatim}
\end{multicols}

\noindent To translate variable references in expressions we followed the following approach.
\begin{multicols}{2}
Global variables
\begin{verbatim}
Scalar or array A

Element A[i] (A is an array)




\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
temp = LABEL_A

temp1 = LABEL_A
temp2 = i
temp3 = sizeof(A's element type) 
temp4 = temp2 * temp3 
temp5 = temp1 + temp4 
\end{verbatim}
\end{multicols}

\begin{multicols}{2}
Local variables
\begin{verbatim}
Array A

Element A[i] (A is an array)





\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
temp = FP+OFFSET_A

temp1 = i
temp2 = sizeof(A's element type) 
temp3 = temp1 * temp2 
temp4 = OFFSET_A 
temp5 = temp4 + temp3 
temp6 = temp5 + FP
\end{verbatim}
\end{multicols}

\noindent To flatten expressions we followed the following approach.
\begin{multicols}{2}
Constants
\begin{verbatim}
c
\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
temp = c
\end{verbatim}
\end{multicols}

\begin{multicols}{2}
Reading value of a variable
\begin{verbatim}
Local variable x 
stored in a temp_x.

Global variable, or local 
variable in memory at ADDR_x
\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
result = temp_x


result = load(ADDR_x)

\end{verbatim}
\end{multicols}

\begin{multicols}{2}
Assigning x = E
\begin{verbatim}
Variable x stored in a temp_x


Global variable, or local 
variable in memory at ADDR_x

\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
temp_e = E
temp_x = temp_e

temp_e = E
store(ADDR_x, temp_e)

\end{verbatim}
\end{multicols}

\begin{multicols}{2}
Unary expressions 
\begin{verbatim}
op(E)

\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
temp = E		
result = op temp	
\end{verbatim}
\end{multicols}

\begin{multicols}{2}
Binary expressions
\begin{verbatim}
E1 op E2


\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
temp1 = E1		
temp2 = E2		
result = temp1 op temp2	
\end{verbatim}
\end{multicols}

\begin{multicols}{2}
Function calls
\begin{verbatim}
f(E1, ..., En)



\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
temp_1 = E1
...
temp_n = En
result = f(temp_1, ..., temp_n)	
\end{verbatim}
\end{multicols}

\subsection*{Translation of control flow statements}
To translate expressions, we implemented the function:
\code{static Temp trans Statement( Statement stmt, RTLSymbolTable symbolTable, List<RtlInsn> \_insns, List<Temp> \_locals)}.
If \code{stmt} is an expression, a temporary \code{Temp} is returned (otherwise the function translates the statement and returns nothing). If the expression is a function call that has \code{void} as return type, the function returns a special temporary called VOID represented by the integer -1.

If \code{stmt} is a \code{if} statement, then the translation is forwarded to the following function:
\lstinputlisting{code-fragments/transIfStmt.txt}	

\begin{multicols}{2}
\noindent \code{if} statement
\begin{verbatim}
   temp1 = E1
   
   
   
   if not temp1 goto Lnext




   
   S1
   
   
Lnext:
\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
Statement condition = 
	(Statement) ifStmt.getChild(0);
	
Temp conTemp = transStatement(condition, 
	symbolTable, _insns, _locals);
CJump cJump = new CJump(false, conTemp, 
	LElse.getLabel());
	_insns.add(cJump);
	
Statement stmt1 = (Statement) ifStmt.getChild(1);	
	transStatement(stmt1,symbolTable, _insns, _locals);

_insns.add(LEnd);
\end{verbatim}
\end{multicols}
\begin{multicols}{2}
\noindent \code{if-else} statement
\begin{verbatim}
   temp1 = E1
   
   
   
   if not temp1 goto L2




   
   S1

   
   goto Lnext
   
   
L2:	

   S2
   
   
Lnext:	
\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
Statement condition = 
	(Statement) ifStmt.getChild(0);
	
Temp conTemp = transStatement(condition, 
	symbolTable, _insns, _locals);
CJump cJump = new CJump(false, conTemp, 
	LElse.getLabel());
	_insns.add(cJump);
	
Statement stmt1 = (Statement) ifStmt.getChild(1);	
	transStatement(stmt1,symbolTable, _insns, _locals);

Jump iJump = new Jump(LEnd.getLabel());
_insns.add(iJump);

_insns.add(LElse);

Statement stmt2 = (Statement) ifStmt.getChild(2);
transStatement(stmt2, symbolTable, _insns, _locals);

_insns.add(LEnd);
\end{verbatim}
\end{multicols}

If \code{stmt} is a \code{while} statement, then the translation is forwarded to the following function:
\lstinputlisting{code-fragments/transWhileStmt.txt}	
\begin{multicols}{2}
\noindent \code{while} statement
\begin{verbatim}
    goto Ltest

    
Lbody:  S



Ltest:  temp = E



    if temp goto Lbody


\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
	Jump iJump = 
	new Jump(Ltest.getLabel());	

LabDef Lbody = new LabDef();
	transStatement(stmt, 
	symbolTable, _insns, _locals);

LabDef Ltest = new LabDef();	
	Temp cExpr = transStatement(
	condition, symbolTable, _insns, _locals);

	CJump cJump = new CJump(true, 
	cExpr, Lbody.getLabel());
	
\end{verbatim}
\end{multicols}

If \code{stmt} is a \code{\&\&} expression, then the translation is forwarded to the following function:
\lstinputlisting{code-fragments/transAndAndExpr.txt}	

\begin{multicols}{2}
\noindent \code{\&\&} expression
\begin{verbatim}
   temp1 = E1

\end{verbatim}
\columnbreak
\begin{verbatim}
resultTemp = transStatement(stmtExpr0, 
symbolTable, _insns, _locals);

\end{verbatim}
\end{multicols}
\begin{multicols}{2}
\begin{verbatim}
   if not temp1 goto L3

\end{verbatim}
\columnbreak
\begin{verbatim}
CJump cJump = new CJump(false, resultTemp, 
falseExpr.getLabel());
\end{verbatim}
\end{multicols}
\begin{multicols}{2}
\begin{verbatim}
   temp1 = E2
   
   
   
   
   
\end{verbatim}
\columnbreak
\begin{verbatim}
Temp expr1Temp = transStatement(stmtExpr1, 
symbolTable, _insns, _locals);
TempExp moveRightHandSide = new TempExp
(expr1Temp);
Eval move = new Eval(resultTemp, 
moveRightHandSide);
\end{verbatim}
\end{multicols}
\begin{multicols}{2}
\begin{verbatim}   
   goto Lnext
\end{verbatim}
\columnbreak
\begin{verbatim}
Jump iJump = new Jump(end.getLabel());
\end{verbatim}
\end{multicols}
\begin{multicols}{2}
\begin{verbatim}      
L3:
\end{verbatim}
\columnbreak
\begin{verbatim}
LabDef falseExpr = new LabDef();
\end{verbatim}
\end{multicols}
\begin{multicols}{2}
\begin{verbatim}
   temp1 = 0
   
   
\end{verbatim}
\columnbreak
\begin{verbatim}
Icon zeroConst = new Icon(0);
Eval moveFalse = new Eval(resultTemp, 
zeroConst);
\end{verbatim}
\end{multicols}
\begin{multicols}{2}
\begin{verbatim}   
Lnext:   
\end{verbatim}
\columnbreak
\begin{verbatim}
LabDef end = new LabDef();
\end{verbatim}
\end{multicols}

If \code{stmt} is a \code{return} statement, then the translation is forwarded to the following function:
\lstinputlisting{code-fragments/transReturnStatement.txt}	
%\newpage
\begin{multicols}{2}
\noindent \code{return} statement
\begin{verbatim}
    temp = E
    
        
    tempRV = temp




    goto Lreturn
    
\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
	Temp resultTemp = transStatement(stmt, 
	symbolTable, _insns, _locals);
	
	TempExp resultTempExpr = new 
	TempExp(resultTemp);
	Eval moveResultToRV = new 
	Eval(Temp.getRV(), resultTempExpr);
	
	Jump iJump = new Jump(symbolTable.
	getReturnLabel());
\end{verbatim}
\end{multicols}

\subsection*{Translation of variable references}
To translate variable references we used the following function:
\begin{lstlisting}
 static Temp transBasicIdentifierExpression(BasicIdentifierExpression idExpr, RTLSymbolTable symbolTable, List<RtlInsn> _insns, List<Temp>_locals)
\end{lstlisting}
(see the source code...)

\begin{multicols}{2}
\noindent \code{return} statement
\begin{verbatim}
    temp = E
    
        
    tempRV = temp




    goto Lreturn
    
\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
	Temp resultTemp = transStatement(stmt, 
	symbolTable, _insns, _locals);
	
	TempExp resultTempExpr = new 
	TempExp(resultTemp);
	Eval moveResultToRV = new 
	Eval(Temp.getRV(), resultTempExpr);
	
	Jump iJump = new Jump(symbolTable.
	getReturnLabel());
\end{verbatim}
\end{multicols}

\begin{multicols}{2}
Local Array A
\begin{verbatim}
temp = FP+OFFSET_A













\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
Temp arrayAddr = new Temp();
Temp offsetTemp = new Temp();

Icon offsetCon = new Icon(localArray.
getOffset());
Eval offsetEval = new Eval(offsetTemp,
 offsetCon);
		       
Binary add2 = new Binary(RtlBinop.PLUS,
 Temp.getFP(), offsetTemp);
Eval arrayAddrEval = new Eval(arrayAddr,
 add2);
returnTemp = arrayAddr;
\end{verbatim}
\end{multicols}

\begin{multicols}{2}
Element A[i] (A is local array)
\begin{verbatim}
temp1 = i

\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
Temp indexTemp = transStatement(indexExp, 
symbolTable, _insns, _locals);
\end{verbatim}
\end{multicols}

\begin{multicols}{2}		
\begin{verbatim}
temp2 = sizeof(A's element type) 




\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
int typeSize = sizeOf(idExprInfo.getRtlType());
Temp typeSizeTemp = new Temp();
Icon typeSizeIcon = new Icon(typeSize);
Eval iconToTemp = new Eval(typeSizeTemp, 
typeSizeIcon);

\end{verbatim}
\end{multicols}

\begin{multicols}{2}
\begin{verbatim}
temp3 = temp1 * temp2 



\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
Binary mul = new Binary(RtlBinop.MUL, 
typeSizeTemp, indexTemp);
Temp mulTemp = new Temp();
Eval mulEval = new Eval(mulTemp, mul);

\end{verbatim}
\end{multicols}

\begin{multicols}{2}
\begin{verbatim}
temp4 = OFFSET_A 





\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
Temp offsetTemp = new Temp();
Icon offsetCon = new Icon(localArray
.getOffset());
Eval offsetEval = new Eval(offsetTemp, 
offsetCon);

\end{verbatim}
\end{multicols}

\begin{multicols}{2}
\begin{verbatim}
temp5 = temp4 + temp3 



\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
Binary add = new Binary(RtlBinop.PLUS, 
mulTemp, offsetTemp);
Temp addTemp = new Temp();
Eval addEval = new Eval(addTemp, add);

\end{verbatim}
\end{multicols}

\begin{multicols}{2}
\begin{verbatim}
temp6 = temp5 + FP






\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}

Temp arrayAddrTemp = new Temp();
Binary add2 = new Binary(RtlBinop.PLUS, 
Temp.getFP(), addTemp);
Eval arrayAddrEval = new Eval(arrayAddrTemp, 
add2);
		    
returnTemp = arrayAddrTemp;
\end{verbatim}
\end{multicols}

\begin{multicols}{2}
Global variable references
\begin{verbatim}
temp = LABEL_A

\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
temp1 = LABEL_A

\end{verbatim}
\end{multicols}

\begin{multicols}{2}
Global array element references
\begin{verbatim}
temp1 = LABEL_A
\end{verbatim}
\columnbreak
translates to:
\begin{verbatim}
GlobalArrayEntryInfo globalArray = 
(GlobalArrayEntryInfo)idExprInfo;
LabRef idLabel = new 
LabRef(globalArray.getLabel().getLabel());
Temp arrayTemp = new Temp();
Eval labelToTemp = new 
Eval(arrayTemp, idLabel);
\end{verbatim}
\end{multicols}

\begin{multicols}{2}
\begin{verbatim}
temp2 = i
\end{verbatim}
\columnbreak
\begin{verbatim}
Statement indexExp =
(Statement)arrayIdent.getChild(0);
Temp indexTemp = transStatement
(indexExp, symbolTable, _insns, _locals);
\end{verbatim}
\end{multicols}

\begin{multicols}{2}
\begin{verbatim}
temp3 = sizeof(A's element type) 
\end{verbatim}
\columnbreak
\begin{verbatim}
int typeSize = sizeOf(idExprInfo.getRtlType());
Temp typeSizeTemp = new Temp();
Icon typeSizeIcon = new Icon(typeSize);
Eval iconToTemp = new 
Eval(typeSizeTemp, typeSizeIcon);
\end{verbatim}
\end{multicols}

\begin{multicols}{2}
\begin{verbatim}
temp4 = temp2 * temp3 
\end{verbatim}
\columnbreak
\begin{verbatim}
Binary mul = new 
Binary(RtlBinop.MUL, typeSizeTemp, indexTemp);
Temp mulTemp = new Temp();
Eval mulEval = new Eval(mulTemp, mul);
\end{verbatim}
\end{multicols}

\begin{multicols}{2}
\begin{verbatim}
temp5 = temp1 + temp4 
\end{verbatim}
\columnbreak
\begin{verbatim}
Binary add = 
new Binary(RtlBinop.PLUS,arrayTemp, mulTemp);
Temp arrayAddrTemp = new Temp();
Eval arrayAddrEval = new 
Eval(arrayAddrTemp, add);
\end{verbatim}
\end{multicols}

\newpage
    \begin{landscape}
    \begin{multicols}{2}
    \section*{Code}
    
    Source code can be accesed at: \code{/home/rogu1637/CompilerPrj/assignment4/} \code{uC-compiler-rtl}
    
	We are using the supplied Java Framework. Based on the supplied files under the \code{absyn} directory, we mainly rewrote or defined the following classes :

	\subsection*{Rtl.java}
	\lstinputlisting{../../rtl/Rtl.java}
	\subsection*{Temp.java}
	\lstinputlisting{../../rtl/Temp.java}
	\subsection*{LabDef.java}
	\lstinputlisting{../../rtl/LabDef.java}
	\subsection*{LabDef.java}
	\lstinputlisting{../../rtl/LabDef.java}
	\subsection*{EntryInfo.java}
	\lstinputlisting{../../rtl/EntryInfo.java}		
	\subsection*{RTLGlobalSymbolTable.java}
	\lstinputlisting{../../rtl/RTLGlobalSymbolTable.java}		
	\subsection*{RTLSymbolTable.java}
	\lstinputlisting{../../rtl/RTLSymbolTable.java}				
    \newpage

\section*{Test Runs}
\subsection*{quiet/lexer/l05.c}
	\lstinputlisting{test-runs/l05.c.txt}	
\subsection*{quiet/parser/p04.c}
	\lstinputlisting{test-runs/p04.c.txt}	
\subsection*{quiet/rtl/r04.c}
	\lstinputlisting{test-runs/r04.c.txt}	
\subsection*{quiet/rtl/r06.c}
	\lstinputlisting{test-runs/r06.c.txt}	
\subsection*{quiet/rtl/r10.c}
	\lstinputlisting{test-runs/r10.c.txt}	
	\end{multicols}
	\end{landscape}
\end{document}
